package com.winit.component.web;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.winit.common.base.web.BaseController;
import com.winit.common.util.Constants;
import com.winit.component.model.ComponentLib;
import com.winit.component.service.ComponentService;
import com.winit.select.service.SelectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by wangjian on 2017/11/23.
 */
@Api(value = "component", tags = {"component"}, description = "页面组件")
@RestController
@RequestMapping("/api/v1/component")
public class ComponentController extends BaseController {
    private static Logger log = LoggerFactory.getLogger(ComponentController.class);

    @Autowired
    ComponentService service;

    @Autowired
    SelectService selectService;

    @RequestMapping(value = "/getAvailableSubElementByElement", method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "查询基础组件信息")
    @ResponseBody
    public Map getWithChild(@ApiParam(value = "组件名.在根下添加时为ROOT", required = true) @RequestParam(value = "component_type", required = true) String component_type) {
        String code = BaseController.REQUEST_SUCCESS;
        ComponentLib lib = service.getComponentLib(component_type, Constants.element_component_index_group, true);
        return this.getRtn(code, "", lib);
    }

    @RequestMapping(value = "/get", method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "查询组件的配置信息")
    @ResponseBody
    public Map get(@ApiParam(value = "组件名称", required = true) @RequestParam(value = "type", required = true) String type) {
        String code = BaseController.REQUEST_SUCCESS;
        Map map = selectService.getSelectConfigs(Constants.element_component_index_group, type);
        return this.getRtn(code, "", map);
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "保存")
    @ResponseBody
    public Map save(@ApiParam(value = "type", required = true) @RequestParam(value = "type", required = true) String type,
                    @ApiParam(value = "列表的 tostring", required = true) @RequestParam(value = "list", required = true) String list) {
        JSONArray array = JSONArray.parseArray(list);
        Map map = new HashMap();
        for (int i = 0; i < array.size(); i++) {
            JSONObject object = array.getJSONObject(i);
            map.put(object.getString("key"), object.getString("value"));
        }
        selectService.saveSelectConfig(Constants.element_component_index_group, type, map);
        return this.getRtn(BaseController.REQUEST_SUCCESS, "");
    }

    @RequestMapping(value = "/getList", method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "获取组件名称列表")
    @ResponseBody
    public Map getComponentList() {
        return this.getRtn(BaseController.REQUEST_SUCCESS, "",service.getComponentList());
    }
}
